package com.mcxx.formulcount.gonshi;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class IfStentence implements ISentence {
    private String privateScript;

    public final String getScript() {
        return privateScript;
    }

    public final void setScript(String value) {
        privateScript = value;
    }


    private String privateJudge;

    public final String getJudge() {
        return privateJudge;
    }

    public final void setJudge(String value) {
        privateJudge = value;
    }


    private java.util.ArrayList<ISentence> TrueSentences = new java.util.ArrayList<ISentence>();
    private java.util.ArrayList<ISentence> FalseSentences = new java.util.ArrayList<ISentence>();

    public final void run(RuntimeContext ctx) throws Exception {
        String judge = getJudge();
        ExpressCalculator exp = new ExpressCalculator(judge, ctx.AllVars());
        double expV = Double.parseDouble(exp.getValue().toString());
        if (expV != 0.0) {
            log.info(">>>表达式命中：{}", judge);
            for (ISentence st : TrueSentences) {
                st.run(ctx);
            }
            log.info("===计算结果：{}", ctx.retVars);
        } else {
            for (ISentence st : FalseSentences) {
                st.run(ctx);
            }
        }
    }

    public final int buildSentence(java.util.ArrayList<String> arr, int p) {
        if (arr.size() - p < 6) {
            log.error("判断表达式错误,位置:" + p);
            throw new RuntimeException("判断表达式错误,位置:" + p);
        }
        if (!arr.get(p + 1).equals("(")) {
            log.error("判断表达式错误,位置:" + p);
            throw new RuntimeException("判断表达式错误,位置" + p);
        }

        java.util.ArrayList<String> exps = new java.util.ArrayList<String>();
        for (int i = p + 1; i < arr.size(); i++) {
            p = i;
            if (arr.get(i).equals("[so]")) {
                break;
            }
            exps.add(arr.get(i));
        }

        setJudge(String.join("", exps));

        int endPos = 0;

        RefObject<Integer> tempRef_endPos = new RefObject<Integer>(endPos);
        java.util.ArrayList<ISentence> sts = BlockServer.build(arr, p + 1, tempRef_endPos);
        endPos = tempRef_endPos.argvalue;

        if (sts.isEmpty()) {
            log.error("判断语句中不能包括空语句,位置" + (p + 1));
            throw new RuntimeException("判断语句中不能包括空语句,位置" + (p + 1));
        }
        TrueSentences.addAll(sts);

        p = endPos + 1;
        if (p >= arr.size()) {
            return p;
        }
        if (arr.get(p).equals("[else]")) {
            RefObject<Integer> tempRef_endPos2 = new RefObject<Integer>(endPos);
            sts = BlockServer.build(arr, p + 1, tempRef_endPos2);
            endPos = tempRef_endPos2.argvalue;

            if (sts.isEmpty()) {
                log.error("判断语句中不能包括空语句,位置" + p);
                throw new RuntimeException("判断语句中不能包括空语句,位置" + p);
            }
            p = endPos + 1;
            FalseSentences.addAll(sts);
        }
        return p;
    }
}
